<template>
  <div>
    <el-dialog :title="`${$t('basic.stage')}：PH_ZL_REC_${tdIndex}`" :visible.sync="isShowREC" :close-on-click-modal="false"
      :before-close="handleClose" :width="screenWidth > 1310 ? '55%' : '70%'" v-dialogDrag>
      <el-form :model="formData" label-width="85px" ref="ruleForm" size="mini" :rules="rules" :hide-required-asterisk="true">

        <el-row :gutter="24" class="gz-form-row">
          <el-col :xs="6" :sm="6" :md="7" :lg="5" :xl="3">
            <el-form-item :label="$t('common.step_number')">
              <el-input v-model="formData.seqNo" disabled placeholder="" style="width: 70px" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="6" :sm="6" :md="6" :lg="4" :xl="3">
            <el-form-item :label="$t('common.order')" label-width="40px">
              <el-input type="number" v-model="formData.stepSort" style="width: 55px"></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="5" :sm="4" :md="4" :lg="3" :xl="3">
            <el-form-item label="" label-width="0">
              <el-checkbox v-model="formData.isActive">{{$t('common.enable')}}</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :xs="5" :sm="4" :md="4" :lg="3" :xl="3">
            <el-form-item label="" label-width="0">
              <el-checkbox v-model="formData.isAuto">{{$t('common.auto_start')}}</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :xs="12" :sm="10" :md="8" :lg="8" :xl="5">
            <el-form-item :label="$t('common.start_delay')" label-width="80px" class="inputPadding">
              <el-input type="number" v-model="formData.delayTime">
                <template slot="append">min</template>
              </el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="5" :sm="4" :md="4" :lg="3" :xl="3">
            <el-form-item label="" label-width="0">
              <el-checkbox v-model="formData.mode">{{$t('common.operation_signature')}}</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :xs="5" :sm="5" :md="5" :lg="3" :xl="3">
            <el-form-item label="" label-width="0">
              <el-checkbox v-model="formData.checks">{{$t('common.review_confirmation')}}</el-checkbox>
            </el-form-item>
          </el-col>
          <!-- <el-col :xs="12" :sm="12" :md="8" :lg="8" :xl="8">
                        <el-form-item :label="$t('basic.stage_name')" prop="phaseDesc">
                            <el-input disabled v-model="formData.phaseDesc" placeholder="" clearable></el-input>
                        </el-form-item>
                    </el-col> -->
          <!-- <el-col :xs="12" :sm="6" :md="2" :lg="2" :xl="2">
                        <el-form-item :label="$t('common.enable')" label-width="60px">
                            <el-checkbox v-model="formData.isAuto" @change="changeCheck"></el-checkbox>
                        </el-form-item>
                    </el-col> -->
          <!-- <el-col :xs="12" :sm="6" :md="6" :lg="6" :xl="6">
                        <el-form-item label="数字签名" label-width="115px">
                            <el-checkbox v-model="formData.signitature" @change="changeCheck"></el-checkbox>
                        </el-form-item>
                    </el-col> -->
        </el-row>
        <!-- <el-row :gutter="24">
                    <el-col  :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="液位L值" prop="levelL" class="append">
                            <el-input type="number" v-model="formData.levelL" placeholder="" v-enterNumber2 clearable>
                                 <template slot="append">mm</template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                    <el-col  :xs="12" :sm="12" :md="12" :lg="12" :xl="12">
                        <el-form-item label="液位H值" prop="levelH" class="append">
                            <el-input type="number" v-model="formData.levelH" placeholder="" v-enterNumber2 clearable>
                                 <template slot="append">mm</template>
                            </el-input>
                        </el-form-item>
                    </el-col>
                </el-row> -->
        <el-row :gutter="35">
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item label-width="0">
              <!-- <el-checkbox v-model="formData.confirm"> -->
              <p style="font-weight: bold">{{$t('recipe.phase_info')}}</p>
              <!-- </el-checkbox> -->
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item :label="$t('common.operating_instructions')">
              <el-input rows="3" type="textarea" v-model="formData.guideInfo" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>
          <el-col :xs="24" :sm="24" :md="24" :lg="24" :xl="24">
            <el-form-item :label="$t('basic.remarks')">
              <el-input type="textarea" rows="3" v-model="formData.flastModifyComment" placeholder="" clearable></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" size="small" :disabled="isreadOnly" :loading="loading"
          @click="handleConfirm('ruleForm')">{{ $t("common.preservation") }}</el-button>
        <el-button @click="handleClose()" size="small">{{ $t("common.cancel") }}</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { formula } from "@/api/formula"
export default {
  props: {
    isShowREC: Boolean,
    tdIndex: Number,
    stepObj: Object,
    isreadOnly: Boolean
  },
  data () {
    let levelHFn = (rule, value, callback) => {
      if (!value) {
        callback(new Error('液位H值不能为空'));
      } else if (parseFloat(value) < parseFloat(this.formData.levelL)) {
        callback(new Error('液位H值不能低于液位L值'));
      } else {
        callback();
      }
    }
    return {
      screenWidth: document.body.clientWidth,
      formData: {
        seqNo: "",
        delayTime: "",
        mode: "",
        checks: "",
        stepNo: "",
        stepDesc: "",
        isActive: false,
        isAuto: false,
        signitature: false,
        phaseDesc: "蒸馏-冷凝接收",
        levelL: "",
        levelH: "",
        confirm: "",
        flastModifyComment: "",
        guideInfo: "",
        stepSort: 0,
      },
      rules: {
        levelH: [{ validator: levelHFn, trigger: 'blur' }],
        levelL: [{ required: true, message: '液位L值不能为空', trigger: 'blur' }],
      },
      loading: false,
      stepLoading: false,
      title: "添加",
      phaseLoading: false,
      phaseId: "",
      stepId1: "",
      stepId2: "",
    }
  },
  watch: {
    screenWidth (val) {
      this.screenWidth = Number(val)
    },
    isShowREC (val) {
      if (val) {
        this.formData.stepDesc = this.stepObj.stepDesc
        this.formData.isActive = this.stepObj.isActive == 1 ? true : false
        this.formData.seqNo = this.stepObj.seqNo
        this.formData.phaseDesc = this.stepObj.texts

        this.formData.stepNo = this.tdIndex
        this.formData.mode = this.stepObj.mode == 1 ? true : false
        this.formData.checks = this.stepObj.checks == 1 ? true : false
        this.formData.delayTime = this.stepObj.delayTime

        // if(this.stepObj.fid){
        //     this.phaseAutoFind()
        // }
        this.formData.isAuto = this.stepObj.isAuto == 1 ? true : false
        this.formData.signitature = this.stepObj.signitature == 1 ? true : false
        this.phaseId = this.stepObj.fid || this.stepObj.fId
        this.formData.confirm = this.stepObj.confirm == 1 ? true : false
        this.formData.flastModifyComment = this.stepObj.flastModifyComment
        this.formData.guideInfo = this.stepObj.guideInfo
        this.formData.stepSort = this.stepObj.stepSort ?? 0
      } else {
        for (let key in this.formData) {
          this.formData[key] = ""
        }
        this.title = "添加"
        this.stepId1 = ""
        this.stepId2 = ""
      }
    }
  },
  mounted () {
    const that = this
    window.addEventListener('resize', function () {
      that.screenWidth = document.body.offsetWidth;
    })
  },
  created () {

  },
  methods: {
    phaseAutoFind () {
      const param = {
        phaseId: this.stepObj.fid
      }
      formula.phaseAutoList(param).then(res => {
        if (res.code == 200) {
          if (res.data.rows.length > 0) {
            this.title = "编辑"
            this.formData.levelL = res.data.rows[0].target
            this.formData.levelH = res.data.rows[1].target
            this.stepId1 = res.data.rows[0].fid
            this.stepId2 = res.data.rows[1].fid
          }

        }
      })
    },
    handleConfirm (formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const param = {
            phaseDesc: this.formData.phaseDesc,
            isAuto: this.formData.isAuto == false ? 0 : 1,
            isActive: this.formData.isActive == false ? 0 : 1,
            checks: this.formData.checks == false ? 0 : 1,
            delayTime: this.formData.delayTime,
            signitature: this.formData.signitature == false ? 0 : 1,
            operationId: this.stepObj.operationId,
            type: this.stepObj.type,
            confirm: this.formData.confirm == false ? 0 : 1,
            flastModifyComment: this.formData.flastModifyComment,
            guideInfo: this.formData.guideInfo,
            mode: this.formData.mode == false ? 0 : 1,
            stepNo: this.stepObj.stepNo,
            seqNo: this.stepObj.seqNo,
            phaseCode: this.stepObj.phaseCode,
            serialNo: this.stepObj.serialNo,
            stepSort: this.formData.stepSort,
          }
          if (this.phaseId) {
            param.fid = this.phaseId
          }
          this.loading = true
          formula.phaseAdd(param).then(res => {
            this.loading = false
            if (res.code == 200) {
              this.$message({ duration: 1000, type: 'success', message: '保存成功!' });
              let obj = {
                mode: this.stepObj.mode,
                isAuto: this.formData.isAuto == false ? 0 : 1,
                isActive: this.formData.isActive == false ? 0 : 1,
                operationId: this.stepObj.operationId,
                seqNo: this.stepObj.seqNo,
                serialNo: this.stepObj.serialNo,
                text: this.stepObj.phaseDesc
              }
              this.$emit("uptionData", obj)
              this.$emit("closePH", "REC")
              // this.handleSubmi()
            } else {
              this.$message.error(res.msg);
              this.loading = false
            }
          })
        }
      })
    },
    handleSubmi () {
      const param = [{
        paraName: "液位L值",
        uom: "mm",
        target: this.formData.levelL,
        phaseId: this.stepObj.fid,
      }, {
        paraName: "液位H值",
        uom: "mm",
        target: this.formData.levelH,
        phaseId: this.stepObj.fid,
      }]
      if (this.title == "编辑") {
        param[0].fid = this.stepId1
        param[1].fid = this.stepId2
      }
      formula.phaseAutoAdd(param, this.title).then(res => {
        this.loading = false
        if (res.code == 200) {
          this.$message({ duration: 1000, type: 'success', message: '保存成功!' });
          let obj = {
            mode: this.stepObj.mode,
            isAuto: this.formData.isAuto == false ? 0 : 1,
            operationId: this.stepObj.operationId,
            seqNo: this.stepObj.seqNo
          }
          this.$emit("uptionData", obj)
          this.$emit("closePH", "REC")
        } else {
          this.$message.error(res.msg);
        }
      })
    },
    handleClose () {
      this.$refs["ruleForm"].resetFields();
      this.$emit("closePH", "REC")
    },
    changeCheck (val) {
      this.$forceUpdate()
    }
  }
}
</script>
<style scoped>
.append >>> .el-input-group__append {
  padding: 0;
  width: 35px;
  text-align: center;
}
</style>
